import { http } from "../http";
import {
  mockGetMaintenancePlanList,
  mockAddOrUpdateMaintenancePlan,
  mockDeleteMaintenancePlan,
} from "../mock/maintenancePlan";

const baseUrl = "/api/dms/maintenancePlans";

// 控制是否使用Mock API的全局变量
const USE_MOCK = false; // 设置为 true 使用Mock，false 使用真实API

// 维护计划状态枚举
export enum MaintenancePlanStatus {
  NOT_STARTED = "NOT_STARTED", // 未开始
  IN_PROGRESS = "IN_PROGRESS", // 进行中
  FINISHED = "FINISHED", // 已完成
}

export interface MaintenancePlanType {
  id: string;
  planName: string;
  planDate: number;
  deviceId: string;
}

export interface GetMaintenancePlansByDateRangeType {
  startDate: number | null;
  endDate: number | null;
  deviceId: string;
}

export interface AddOrUpdateMaintenancePlanQueryType {
  id?: string | null;
  planName: string;
  deviceId: string;
  planDate: number;
  status: MaintenancePlanStatus;
}

// 获取维护计划列表
export const getAllMaintenancePlansAPI = (
  params: GetMaintenancePlansByDateRangeType
) => {
  if (USE_MOCK) {
    return mockGetMaintenancePlanList();
  }
  return http.post(`${baseUrl}/getMaintenancePlansByDateRange`, params);
};

// 分页获取维护计划列表
export interface GetMaintenancePlansByPageRequest {
  page: number;
  size: number;
  planName?: string;
  deviceId?: string;
  status?: string;
  startDate?: number | null;
  endDate?: number | null;
}
export const getMaintenancePlansByPageAPI = (
  params: GetMaintenancePlansByPageRequest
) => {
  return http.post(`${baseUrl}/getMaintenancePlansByPage`, params);
};

// 添加或编辑维护计划
export const addMaintenanceContractAPI = (
  params: AddOrUpdateMaintenancePlanQueryType
) => {
  if (USE_MOCK) {
    return mockAddOrUpdateMaintenancePlan(params);
  }
  return http.post(`${baseUrl}/addOrUpdateMaintenancePlan`, params);
};

// 删除维护计划
export const deleteMaintenancePlanAPI = (id: string) => {
  if (USE_MOCK) {
    return mockDeleteMaintenancePlan(id);
  }
  return http.delete(`${baseUrl}/deleteMaintenancePlan/${id}`);
};
